import {
	nanoid
} from 'nanoid/non-secure'
// 时间戳转换
// return yyyy/MM/dd HH:mm:ss
function formatTimestamp(timestamp) {
	if (timestamp == null || timestamp == undefined) {
		return ''
	}
	const date = new Date(timestamp);
	// return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');
	return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
}
// return array[0]:yyyy/MM/dd array[1]:HH:mm:ss
function formatTimestampToStrArray(timestamp) {
	let array = ['', '']
	if (timestamp == null || timestamp == undefined) {
		return array
	}
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');
	array[0] = `${year}/${month}/${day}`
	array[1] = `${hours}:${minutes}:${seconds}`
	// array[0] = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
	// array[1] = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
	return array;
}
// return yyyy/MM/dd
function formatTimestampToDateStr(timestamp) {
	if (timestamp == null || timestamp == undefined) {
		return ''
	}
	const date = new Date(timestamp);
	// return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	return `${year}/${month}/${day}`
}
// return HH:mm:ss
function formatTimestampToDateTimeStr(timestamp) {
	if (timestamp == null || timestamp == undefined) {
		return ''
	}
	const date = new Date(timestamp);
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');
	// return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
	return `${hours}:${minutes}:${seconds}`
}

// 分转元
function regFenToYuan(fen) {
	if (fen == null || fen == undefined) {
		return ''
	}
	var num = fen;
	num = fen * 0.01;
	num += '';
	var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
	num = num.replace(reg, '$1');
	num = toDecimal2(num)
	return num
}
// 强制保留2位小数，如：2，会在2后面补上00.即2.00
function toDecimal2(x) {
	var f = parseFloat(x);
	if (isNaN(f)) {
		return false;
	}
	var f = Math.round(x * 100) / 100;
	var s = f.toString();
	var rs = s.indexOf('.');
	if (rs < 0) {
		rs = s.length;
		s += '.';
	}
	while (s.length <= rs + 2) {
		s += '0';
	}
	return s;
}
/**
 * yuan: 要转换的钱，单位元
 * digit：转换倍数
 */
function regYuanToFen(yuan, digit) {
	let m = 0
	let s1 = yuan.toString()
	let s2 = digit.toString()
	try {
		m += s1.split('.')[1].length
	} catch (e) {}
	try {
		m += s2.split('.')[1].length
	} catch (e) {}
	return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}

// 更新订单状态-调用支付的notifyUrl：zhy-pay-notify
function refreshOrderStatus(orderId) {
	let param = {
		// 标识id
		zhy_batchId: nanoid(18),
		zhy_batchSize: 100, //一次性查询需要更新状态的订单数量
	}
	if (orderId != undefined && orderId != null && orderId != '') {
		param.zhy_orderId = orderId
	}
	uniCloud.callFunction({
		name: 'zhy-pay-notify',
		data: param
	})
}

function showSuccessToast(msg = '成功', icon = 'none') {
	console.log('msg:' + msg);
	uni.showToast({
		title: msg,
		icon: icon
	})
}

function showErrorToast(msg = '失败', icon = 'none') {
	uni.showToast({
		title: msg,
		icon: icon
	})
}

//导出
module.exports = {
	formatTimestamp,
	formatTimestampToStrArray,
	formatTimestampToDateStr,
	formatTimestampToDateTimeStr,
	regFenToYuan,
	regYuanToFen,
	refreshOrderStatus,
	showSuccessToast,
	showErrorToast,
}